package com.sxhalo.fsms.util;

import java.util.Date;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

public class TokenUtil {

	public static String secret = "fsms.com";

	public static Long expiration = 28800l;//8小时
	
	public static String createToken(Integer userId, Integer roleId) {

		String token=Jwts.builder().claim("userId", userId).claim("roleId", roleId)
					.setExpiration(new Date(System.currentTimeMillis() + expiration * 1000))
					.setIssuedAt(new Date(System.currentTimeMillis()))
					.signWith(SignatureAlgorithm.HS512, secret)
					.compressWith(CompressionCodecs.GZIP)
					.compact();

		return token;
	}

	public static Integer getUserId(String token) {
		String userId = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody().get("userId").toString();
		return Integer.parseInt(userId);
	}

	public static Integer getRoleId(String token) {
		String roleId = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody().get("roleId").toString();
		return Integer.parseInt(roleId);
	}

	public static Boolean verifyToken(String token) {
		Claims claims;
		try {
			claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
		} catch (Exception e) {
			claims = null;
		}
		return claims != null;
	}

}
